#include "alarm.h"
#include "collection.h"
#include "db.h"
#include "global.h"
#include "operation.h"
#include "setting.h"
#include "u.h"
#include <qdebug.h>
#include <QThreadPool>

Collection::Collection(QObject *parent)
    : QObject(parent), m_temperature(0.0),m_weight(0.0), m_PH(0.0), m_speed(0.0), m_conductivity(0.0), m_result(Global::Result::Zero), m_count(0),m_periodOfRecord(3600),
    m_temperatureColor(2), m_weightColor(2), m_speedColor(2), m_PHColor(2), m_conductivityColor(2)
{
    m_pWorker = new Worker(parent);
    connect(m_pWorker, SIGNAL(signalUpdate(float, float, float, float, float, float, float, float, float)),
            this, SLOT(slotUpdate(float, float, float, float, float, float, float, float, float)));
    connect(m_pWorker, SIGNAL(signalInfo(Global::Cmd, Global::Result, QString)), this, SLOT(slotInfo(Global::Cmd, Global::Result, QString)));
    connect(this, SIGNAL(signalUpdateParam(const QString&, const QString&)), m_pWorker, SLOT(slotUpdateParam(const QString&, const QString&)));


    m_pTrigger = new Trigger(parent);
    connect(m_pTrigger, SIGNAL(signalInfo(Global::Cmd, Global::Result, QString)), this, SLOT(slotInfo(Global::Cmd, Global::Result, QString)));

    //m_pWorker->start();
    // qDebug() <<U::getThDt()<< ">>>>>>>>>>>>>>>>>>启动完成-线程状态"<< m_pWorker->isRunning()<<">>"<<m_pWorker->isFinished();


    //m_pWorker->moveToThread(m_pThread);
    //connect(this,SIGNAL(signalDoJob()), m_pWorker,SLOT(slotDoJob()));
    //m_pThread->start();
    //QThreadPool::globalInstance()->start(m_pWorker);
    //emit signalDoJob();
}

void Collection::start()
{
    Setting* pSetting = (Setting*)Global::instance().get("settingInfo");
    connect(pSetting, SIGNAL(signalUpdateParam(const QString&, const QString&)), this, SLOT(slotUpdateParam(const QString&, const QString&)));
    QString dataSamplingPeriod = pSetting->getSettingParam("system_dataSamplingPeriod");
    QString weightDecimalPlaces= pSetting->getSettingParam("system_decimalPlaces");
    // int second = Global::instance().strToSecond(dataSamplingPeriod);
    emit signalUpdateParam("system_dataSamplingPeriod", dataSamplingPeriod);
    emit signalUpdateParam("system_decimalPlaces", weightDecimalPlaces);
    m_pWorker->start();
    qDebug() <<U::getThDt()<< " worker"<< m_pWorker->isRunning()<<">>"<<m_pWorker->isFinished();
    m_pTrigger->start();
    qDebug() <<U::getThDt()<< "trigger"<< m_pTrigger->isRunning()<<">>"<<m_pTrigger->isFinished();
}

bool Collection::calibrationWeight(int weight)
{
    Command c;
    c.m_cmd=Global::Cmd::WeightCalibration_weight;
    c.m_data = weight;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("称重砝码校准","校准成功", QString("%1").arg(weight));
    return true;
}

bool Collection::calibration0()
{
    Command c;
    c.m_cmd=Global::Cmd::WeightCalibration_0;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("称重0点校准","校准成功", "");
    return true;
}

bool Collection::setCollect(bool run)
{
    m_pWorker->setRun(run);
    return true;
}

bool Collection::toZero()
{
    Command c;
    c.m_cmd=Global::Cmd::WeightToZero;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("称重归零","归零成功", "");
    return true;
}

bool Collection::toTare()
{
    Command c;
    c.m_cmd=Global::Cmd::WeightToTare;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("称重去皮","去皮成功", "");
    return true;
}

bool Collection::setSpeed(int speed)
{
    Command c;
    c.m_cmd=Global::Cmd::SpeedSet;
    c.m_data = speed;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("设置速度",QString("设置速度[%1]").arg(speed), "");
    return true;
}

bool Collection::phCalibration1()
{
    Command c;
    c.m_cmd=Global::Cmd::PhCalibration_1;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("pH1点校准","校准成功", "");
    return true;
}

bool Collection::phCalibration2()
{
    Command c;
    c.m_cmd=Global::Cmd::PhCalibration_2;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("pH2点校准","校准成功", "");
    return true;
}

bool Collection::conductivityCalibration2()
{
    Command c;
    c.m_cmd=Global::Cmd::ConductivityCalibration_2;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("电导2点校准","校准成功", "");
    return true;
}

bool Collection::setBuzzing(bool bBuzzing)
{
    m_pTrigger->setBuzzing(bBuzzing);
    return true;
}

bool Collection::showInfo(const QString &info)
{
    m_info=info;
    m_cmd = Global::Cmd::Validator;
    m_result = Global::Result::Success;
    // emit signalDataChanged();
    return true;
}
bool Collection::conductivityCalibration1()
{
    Command c;
    c.m_cmd=Global::Cmd::ConductivityCalibration_1;
    m_pWorker->addCmd(c);
    Operation* pOperation =  (Operation*)Global::instance().get("operationInfo");
    pOperation->insertOperationLog("电导1点校准","校准成功", "");
    return true;
}
void Collection::slotUpdate(float speed, float temperature, float weight, float ph, float phTemp, float phUsageTime,
                            float conductivity, float conTemp, float conUsageTime)
{
    m_temperature = temperature;
    m_weight = weight;
    m_PH=ph;
    m_phTemp = phTemp;
    m_phUsageTime = phUsageTime;
    m_speed = speed;
    m_conductivity = conductivity;
    m_conductivityTemp = conTemp;
    m_conductivityUsageTime = conUsageTime;
    //qDebug() << U::getThDt() << "temp:"<< temperature <<">weight:"<< weight;
    Alarm* pAlarm       =  (Alarm*)Global::instance().get("alarmInfo");

    m_speedColor        = pAlarm->checkAlarm("speed", "速度", m_speed);
    m_temperatureColor  = pAlarm->checkAlarm("temperature", "温度", m_temperature);
    m_weightColor       = pAlarm->checkAlarm("weight", "重量", m_weight);
    m_PHColor           = pAlarm->checkAlarm("ph", "pH", m_PH);
    m_conductivityColor = pAlarm->checkAlarm("conductivity", "电导", m_conductivity);
    m_count++;
    if (m_count == m_periodOfRecord){
        DB::getInstance()->insertBatchData("",QString("%1").arg(speed), QString("%1").arg(weight),QString("%1").arg(temperature),QString("%1").arg(ph), QString("%1").arg(conductivity));
        m_count =0;
    }

    emit signalDataChanged();
}

void Collection::slotInfo(Global::Cmd cmd, Global::Result result, QString info)
{
    m_info=info;
    m_result = result;
    m_cmd = cmd;
    qDebug() << __FILE__ << __FUNCTION__ << result << cmd << info;
    emit signalInfoChanged();
}

void Collection::slotUpdateParam(const QString& param, const QString& value)
{
    qDebug() <<U::getThDt()<< ">>" << param << value;
    if (param.compare("system_dataSamplingPeriod") ==0 ){
        m_periodOfRecord = Global::instance().strToSecond(value);
    }
    emit signalUpdateParam(param, value);
}

int Collection::speedColor()
{
    return m_speedColor;
}
int Collection::temperatureColor()
{
    return m_temperatureColor;
}
int Collection::weightColor()
{
    return m_weightColor;
}
int Collection::phColor()
{
    return m_PHColor;
}
int Collection::conductivityColor()
{
    return m_conductivityColor;
}

QString Collection::temperature()
{
    return QString::number(m_temperature);
}

QString Collection::weight()
{
    return QString::number(m_weight);
}

QString Collection::ph()
{
    return QString::number(m_PH);
}

QString Collection::phTemp()
{
    return QString::number(m_phTemp);
}

QString Collection::phUsageTime()
{
    return QString::number(m_phUsageTime);
}

QString Collection::conTemp()
{
    return QString::number(m_conductivityTemp);
}

QString Collection::conUsageTime()
{
    return QString::number(m_conductivityUsageTime);
}

QString Collection::speed()
{
    return QString::number(m_speed);
}

QString Collection::conductivity()
{
    return QString::number(m_conductivity);
}

QString Collection::info()
{
    return m_info;
}

Global::Result Collection::result()
{
    return m_result;
}
Global::Cmd Collection::cmd()
{
    return m_cmd;
}
